package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Slf4j
//@CrossOrigin
@Api(tags = "数字字典")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation("根据id查询子列表")
    @GetMapping("findChildData/{id}")
    public Result findByParentId(@PathVariable Long id){
       List<Dict> dictList= dictService.haveChilsData(id);
       log.info("查询得数据为{}",dictList);
       return Result.ok(dictList);
    }
    //数据导出
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse Response){
        dictService.exportDataServuce(Response);
    }
    //数据得导入
    @PostMapping("/importData")
    public Result importData(MultipartFile file){
        String name = file.getName();
        log.info("是否进来{}",name);
        dictService.importExceData(file);
        return Result.ok();
    }
    //取数据字典名称 使用parentDictCode和value
    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getName(@PathVariable String parentDictCode,
                                                  @PathVariable String value){
        log.info("字典的parentDictCode{}，value{}",parentDictCode,value);
       String name= dictService.getNameByParentDictCodeAndValue(parentDictCode,value);
        log.info("字典的name1{}",name);
       return  name;
    }
    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{value}")
    public String getName(@PathVariable String value){
        String name= dictService.getNameByParentDictCodeAndValue("",value);
        log.info("字典的name2{}",name);
        return  name;
    }
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
        //通过dictCode获取一个dict的list集合
        List<Dict> list =dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }
}
